package com.print.androidprintproject.page;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import com.print.androidprintproject.R;
import com.print.androidprintproject.print.PrintJobService;
import com.print.printlib.export.preDefiniation;

public class BarcodePrintActivity extends BaseActivity implements
        OnItemSelectedListener,
        android.widget.CompoundButton.OnCheckedChangeListener, OnClickListener {
    private RadioButton rbBarCode1;
    private RadioButton rbBarCode2;
    private Spinner spBarCodeType;
    private Spinner spContent;
    private int barType = 1;
    private int barNote = 1;
    private EditText etWidth;
    private EditText etHeight;
    private EditText etCodeContent;
    private TextView tvShowWidth;
    private TextView tvShowHeight;
    private TextView tvWidth;
    private TextView tvHeigh;
    private TextView tvQRSize;
    private Button btnSend;
    private Context mContext;
    private ArrayAdapter<CharSequence> barTypeAdapter;
    private ArrayAdapter<CharSequence> barNoteAdapter;
    private final static int SCANNIN_GREQUEST_CODE = 2;
    private LinearLayout header;
    private String TAG = "com.printer.demo.ui";
    private PrintJobService printJobService;

    protected void onCreate(android.os.Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_print_barcode);
        init();
        if (rbBarCode1.isChecked()) {
            barTypeAdapter = ArrayAdapter.createFromResource(
                    BarcodePrintActivity.this, R.array.barcode1,
                    android.R.layout.simple_spinner_item);
            // 设置样式
            barTypeAdapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spBarCodeType.setAdapter(barTypeAdapter);
            barNoteAdapter = ArrayAdapter.createFromResource(
                    BarcodePrintActivity.this, R.array.barNoteArray,
                    android.R.layout.simple_spinner_item);
            // 设置样式
            barNoteAdapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spContent.setAdapter(barNoteAdapter);
            rbBarCode2.setChecked(false);

        }
        printJobService = PrintJobService.getObject(this, null);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void init() {
        mContext = BarcodePrintActivity.this;
        header = findViewById(R.id.ll_header_barcode);
        initHeader();
        // 初始化radioButton并给它设置选择监听事件
        rbBarCode1 = findViewById(R.id.rb_58mm);
        rbBarCode2 = findViewById(R.id.rb_80mm);
        rbBarCode1.setOnCheckedChangeListener(this);
        rbBarCode2.setOnCheckedChangeListener(this);
        // 初始化spinner并给他设置子项选择监听事件
        spBarCodeType = findViewById(R.id.spinner_barcode_type);
        spContent = findViewById(R.id.spinner_interface_type);
        spBarCodeType.setOnItemSelectedListener(this);
        spContent.setOnItemSelectedListener(this);
        // 初始化设置条码宽度和高度以及输入条码内容的Edittext，
        etHeight = findViewById(R.id.et_input_height);
        etWidth = findViewById(R.id.et_input_width);
        etCodeContent = findViewById(R.id.et_barcode_content);
        // 初始化显示条码宽度和高度的范围的textview
        tvShowWidth = findViewById(R.id.tv_input_width);
        tvShowHeight = findViewById(R.id.tv_input_height);
        tvWidth = findViewById(R.id.tv_barcode_width);
        tvHeigh = findViewById(R.id.tv_barcode_height);
        tvQRSize = findViewById(R.id.tv_barcode_setting);

        // 初始化Button按钮并给他们设置点击监听事件
        btnSend = findViewById(R.id.btn_barcodedata_send);
        btnSend.setOnClickListener(this);
    }

    /**
     * 初始化标题上的信息
     */
    private void initHeader() {
        setHeaderLeftText(header, getString(R.string.back),
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
        headerConnecedState.setText(getTitleState());
        setHeaderLeftImage(header, new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setHeaderCenterText(header, getString(R.string.headview_BarcodePrint));
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if (parent == spBarCodeType) {
            barType = position;
        } else if (parent == spContent) {
            if (rbBarCode1.isChecked()) {
                barNote = position;
            } else {
                barNote = position;
            }
        }
        initUI();
    }

    /**
     * 选择不同的条码，取值范围不同
     */
    private void initUI() {
        boolean isDimOneChecked = rbBarCode1.isChecked();
        if (isDimOneChecked) {
            tvWidth.setText(R.string.barcode_width);
            tvShowWidth.setText(R.string.width_data_range);
            etWidth.setText(R.string.barcode_width_default_value);
            tvHeigh.setText(R.string.barcode_height);
            tvShowHeight.setText(R.string.height_data_range);
            etHeight.setText(R.string.barcode_height_default_value);
            tvQRSize.setText(R.string.content_position);
            if (barType == 6) {
                // UPC_E编码
                etCodeContent.setText(R.string.upc_e_default_value);
            } else if (barType == 7) {
                // EAN13编码
                etCodeContent.setText(R.string.ean_13_default_value);

            } else if (barType == 8) {
                // EAN8编码
                etCodeContent.setText(R.string.ean_8_default_value);

            } else if (barType == 9) {
                // 全部格式的编码 适配 UPC_E,EAN13,EAN8等所有对内容格式有要求的编码
                etCodeContent.setText(R.string.upc_e_default_value);
            } else {
            }

        } else {
            if (barType == 0) {
                tvWidth.setText(R.string.version);
                tvShowWidth.setText(R.string.verion_range);
                etWidth.setText(R.string.characters_default_value);
                tvShowHeight.setText(R.string.level_range);
                tvHeigh.setText(R.string.level);
                etHeight.setText(R.string.level_default_value);
                tvQRSize.setText(R.string.multiple);

            } else if (barType == 1) {
                tvWidth.setText(R.string.characters_in_line);
                tvShowWidth.setText(R.string.characters_range);
                etWidth.setText(R.string.version_default_value);
                tvHeigh.setText(R.string.level);
                tvShowHeight.setText(R.string.level_range_qrcode);
                etHeight.setText(R.string.level_range_default_value_qrcode);
                tvQRSize.setText(R.string.multiple);
            } else if (barType == 2) {
                tvWidth.setText(R.string.barcode_width);
                tvShowWidth
                        .setText(R.string.barcode_width_data_range_datamatrix);
                etWidth.setText(R.string.level_range_default_value_qrcode);
                tvHeigh.setText(R.string.barcode_height);
                tvShowHeight
                        .setText(R.string.barcode_height_data_range_datamatrix);
                etHeight.setText(R.string.barcode_height_default_value_datamatrix);
                tvQRSize.setText(R.string.multiple);
            }
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }

    /**
     * 选择一维码或者二维码，条码列表不同
     */

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // 一维码
        if (buttonView == rbBarCode1 && isChecked) {
            barTypeAdapter = ArrayAdapter.createFromResource(
                    BarcodePrintActivity.this, R.array.barcode1,
                    android.R.layout.simple_spinner_item);
            // 设置样式
            barTypeAdapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spBarCodeType.setAdapter(barTypeAdapter);

            barNoteAdapter = ArrayAdapter.createFromResource(
                    BarcodePrintActivity.this, R.array.barNoteArray,
                    android.R.layout.simple_spinner_item);
            // 设置样式
            barNoteAdapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spContent.setAdapter(barNoteAdapter);
            rbBarCode2.setChecked(false);
            // 二维码
        } else if (buttonView == rbBarCode2 && isChecked) {
            barTypeAdapter = ArrayAdapter.createFromResource(
                    BarcodePrintActivity.this, R.array.barcode2,
                    android.R.layout.simple_spinner_item);
            // 设置样式
            barTypeAdapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spBarCodeType.setAdapter(barTypeAdapter);

            barNoteAdapter = ArrayAdapter.createFromResource(
                    BarcodePrintActivity.this, R.array.barEnlargeArray,
                    android.R.layout.simple_spinner_item);
            // 设置样式
            barNoteAdapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spContent.setAdapter(barNoteAdapter);
            rbBarCode1.setChecked(false);
        }
        spContent.setSelection(2, true);
        initUI();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            // 发送数据并打印
            case R.id.btn_barcodedata_send:
                String content = etCodeContent.getText().toString();
                if (rbBarCode1.isChecked()) {
                    int width = Integer.parseInt(etWidth.getText().toString());
                    int height = Integer.parseInt(etHeight.getText().toString());
                    if (width < 2 || width > 6) {
                        width = 2;
                    }
                    if (height < 1 || width > 255) {
                        height = 200;
                    }
                    preDefiniation.BarcodeType barcodeType = preDefiniation.BarcodeType.BT_CODE128;
                    switch (barType) {
                        case 0:
                            barcodeType = preDefiniation.BarcodeType.BT_CODE128;
                            break;
                        case 1:
                            barcodeType = preDefiniation.BarcodeType.BT_CODE39;
                            break;
                        case 2:
                            barcodeType = preDefiniation.BarcodeType.BT_CODABAR;
                            break;
                        case 3:
                            barcodeType = preDefiniation.BarcodeType.BT_CODEITF;
                            break;
                        case 4:
                            barcodeType = preDefiniation.BarcodeType.BT_CODE93;
                            break;
                        case 5:
                            barcodeType = preDefiniation.BarcodeType.BT_UPCA;
                            break;
                        case 6:
                            barcodeType = preDefiniation.BarcodeType.BT_UPCE;
                            break;
                        case 7:
                            barcodeType = preDefiniation.BarcodeType.BT_EAN13;
                            break;
                        case 8:
                            barcodeType = preDefiniation.BarcodeType.BT_EAN8;
                            break;
                    }
                    preDefiniation.Barcode1DHRI barcode1DHRI = preDefiniation.Barcode1DHRI.BH_BLEW;
                    switch (barNote) {
                        case 0:
                            barcode1DHRI = preDefiniation.Barcode1DHRI.BH_NO;
                            break;
                        case 1:
                            barcode1DHRI = preDefiniation.Barcode1DHRI.BH_UNDER;
                            break;
                        case 2:
                            barcode1DHRI = preDefiniation.Barcode1DHRI.BH_BLEW;
                            break;
                    }
                    printJobService.print1DBarcode(barcodeType, width, height, barcode1DHRI, content);
                } else {
                    int version = Integer.parseInt(etWidth.getText().toString());
                    int ecc = Integer.parseInt(etHeight.getText().toString());
                    int size = barNote;
                    printJobService.print2DBarcode(preDefiniation.BarcodeType.BT_QRcode, size, version, ecc, content);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK)
            return;
    }
}